package cn.xorange.commons.security.service;

import cn.xorange.commons.security.constants.HeaderConstants;
import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.exception.SecurityException;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author : yangjian
 * @date : 2023/11/25
 * @description :
 */
@Service
public class UserDetailsServiceImpl {
    @Autowired
    HttpServletRequest request;
    @Resource
    LoginHandlerFactory loginHandlerFactory;

    public SecurityUser loadUserByUsername(JSONObject object) {
        String sysType = object.getString(HeaderConstants.X_SYS_TYPE);
        String loginType = object.getString(SecurityConstants.LOGIN_TYPE);
        LoginHandler loginHandler;
        try {
            loginHandler = loginHandlerFactory.getLoginHandler(sysType,loginType);
        }catch (Exception e){
            throw SecurityException.loginHandlerNotFound();
        }
        
        return loginHandler.handle(object);
    }
}
